package servlet;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("注册");
        try {
            //解决乱码
            req.setCharacterEncoding("UTF-8");
            resp.setContentType("text/html;charset=utf-8");
            resp.setHeader("content-type","text/html;charset=utf-8");
            // 接收前端提交过来的 参数,通过输入框name属性值
            String name = req.getParameter("name");
            String password = req.getParameter("password");
            String type = req.getParameter("type");
            String sex = req.getParameter("sex");
            String read = req.getParameter("read");
            String music = req.getParameter("music");
            String sport = req.getParameter("sport");
            String game = req.getParameter("game");
            String text = req.getParameter("email");

            System.out.println("姓名：" + name);
            System.out.println("密码：" + password);
            System.out.println("爱好：" +  read + "--" + music + "--" + sport + "--" + game);
            System.out.println("性别：" + sex);
            System.out.println("用户类型:" + type);
            System.out.println("email：" + text);

            String url = "jdbc:mysql:///face_web?characterEncoding=utf8&useSSL=false";
            Class.forName("com.mysql.jdbc.Driver");//加载数据驱动
            Connection con = DriverManager.getConnection(url,"root","root");
            Statement statement = con.createStatement();
           // String sql = "insert into user(name,password) value('"+name+"','"+password+"','"+type+"','"+sex+"','"+read+"','"+music+"','"+sport+"','"+game+"','"+email+"')";
            String sql = "insert into user(name,password,sex,type,email) value('"+name+"','"+password+"','"+sex+"','"+type+"','"+text+"')";
            statement.execute(sql);
            resp.sendRedirect("./login.jsp");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            // 3、TODO关闭资源
        }
        // 4、给前端响应
    //    resp.getWriter().write("<a href='http://localhost:8089/face_web_war_exploded/login.jsp'>注册成功了,跳转首页</a>");
    }

}
